// entry
'use strict'
// 引入express 请求体参数获取 数据库orm session操作
const express = require( 'express' );
const bodyParser = require( 'body-parser' );
const orm = require( 'orm' );
const session = require( 'express-session' );

// 创建应用``
const app = express();

// 初始化session
app.use( session( {
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true
} ) );

// post 获取请求体参数
app.use( bodyParser.urlencoded( {
  extended: false
} ) );
app.use( bodyParser.json() );

// 连接数据库
app.use( orm.express( 'mysql://root:admin@127.0.0.1/videomanage', {
  define: function ( db, models, next ) {
    console.log( '连接数据库成功' );
    // 用户
    models.user = db.define( 'users', {
      uid: {
        type: 'serial',
        key: true
      },
      uname: String,
      upwd: String,
      status: String // 一定和数据库名称一样吗
    } );

    // 视频
    models.video = db.define( 'videos', {
      vid: {
        type: 'serial',
        key: true
      },
      vtitle: String,
      vsortno: Number,
      vvideoid: String,
      vsummary: String,
      vimgurl: String,
      vremark: String,
      vstatus: Number
    } );

    next();
  }
} ) );




// 设置静态资源
app.use( express.static( './statics' ) );

// 路由管理
// 1. 账号相关路由
const accountRouter = require( './routers/accountRouter.js' );
app.use( '/account', accountRouter );




// 开启监听
app.listen( 5566, '127.0.0.1', ( err ) => {
  if ( err ) {
    console.log( err );
    return;
  }
  console.log( 'start success' );
} )
